const sqlite3 = require('../server/node_modules/sqlite3').verbose();
const path = require('path');

// 检查数据库中user_lesson_progress表的time_spent字段
const dbPath = path.join(__dirname, '../resources/db/db.database');
console.log('Database path:', dbPath);

const db = new sqlite3.Database(dbPath);

console.log('\n=== Before Update - User Lesson Progress Data ===');
db.serialize(() => {
  db.all("SELECT * FROM user_lesson_progress", (err, rows) => {
    if (err) {
      console.error('Error:', err);
    } else {
      console.log('User lesson progress rows:');
      rows.forEach(row => {
        console.log(`ID: ${row.id}, User ID: ${row.user_id}, Lesson ID: ${row.lesson_id}, Course ID: ${row.course_id}, Time Spent: ${row.time_spent}, Completion: ${row.completion_percentage}%`);
      });
    }
    
    // 模拟更新操作，增加time_spent
    console.log('\n=== Simulating Update with time_spent ===');
    const updateQuery = `
      UPDATE user_lesson_progress 
      SET time_spent = time_spent + 120, updated_at = CURRENT_TIMESTAMP
      WHERE id = 1
    `;
    
    db.run(updateQuery, function(err) {
      if (err) {
        console.error('Error updating time_spent:', err);
      } else {
        console.log(`Updated ${this.changes} row(s)`);
        
        // 检查更新结果
        console.log('\n=== After Update - User Lesson Progress Data ===');
        db.all("SELECT * FROM user_lesson_progress WHERE id = 1", (err, rows) => {
          if (err) {
            console.error('Error:', err);
          } else {
            console.log('Updated row:');
            rows.forEach(row => {
              console.log(`ID: ${row.id}, User ID: ${row.user_id}, Lesson ID: ${row.lesson_id}, Course ID: ${row.course_id}, Time Spent: ${row.time_spent}, Completion: ${row.completion_percentage}%`);
            });
          }
          
          db.close();
        });
      }
    });
  });
});